﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Text.RegularExpressions;

namespace Lottery
{
    class Types
    {
        /// <summary>
        /// 是否空值
        /// </summary>
        /// <param name="text">字符串</param>
        /// <returns>bool结果</returns>
        public static bool IsNullOrEmpty(string text)
        {
            if (text == null || (text != null && text.Length == 0))
                return true;
            return false;
        }

        /// <summary>
        ///验证Email格式是否正确
        /// </summary>
        public static bool isValidEmail(string value)
        {
            bool result = false;
            try
            {
                result = Regex.IsMatch(value, @"^([\w-\.]+)@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([\w-]+\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$");
            }
            catch (Exception ex)
            {
                LogService.ErrorLog("Common::isValidEmail(" + value + ")", ex);
            }
            return result;
        }

        /// <summary>
        /// 转 Double
        /// </summary>
        /// <param name="value">object</param>
        /// <returns>返回double，转换失败返回0</returns>
        public static double ToDouble(object value)
        {
            double rtn = 0.00;
            if (value.ToString() != "")
                try
                {
                    rtn = Convert.ToDouble(value);
                }
                catch (Exception ex)
                {
                    LogService.ErrorLog("Common::getDouble(" + value.ToString() + ")", ex);
                }
            else
                LogService.ErrorLog("Common::getDouble(" + value.ToString() + ")");
            return rtn;
        }

        /// <summary>
        /// 转 Int32
        /// </summary>
        /// <param name="value">object</param>
        /// <returns>返回Int32，转换失败返回0</returns>
        public static int ToInt32(object value)
        {
            Int32 rtn = 0;
            if (value.ToString() != "")
                try
                {
                    rtn = Convert.ToInt32(value);
                }
                catch (Exception ex)
                {
                    LogService.ErrorLog("Common::ToInt32(" + value.ToString() + ")", ex);
                }
            else
                LogService.ErrorLog("Common::ToInt32(" + value.ToString() + ")");
            return rtn;
        }

        public static DateTime ToDateTime(object value)
        {
            DateTime rtn = new System.DateTime();
            if (value.ToString() != "")
                try
                {
                    rtn = Convert.ToDateTime(value);
                }
                catch (Exception ex)
                {
                    LogService.ErrorLog("Common::ToDateTime(" + value.ToString() + ")", ex);
                }
            else
                LogService.ErrorLog("Common::ToDateTime(object " + value.ToString() + ")");
            return rtn;
        }

        public static string ToFullPath(string path)
        {
            string result = "";
            if (path.IndexOf(@":\\") == -1)
                //filename = System.Environment.CurrentDirectory + "\\" + iniFilename;
                result = System.AppDomain.CurrentDomain.BaseDirectory + path;
            else
                result = path;
            return path;
        }
    }
}
